
const NoteSchema = require('../models/Note.js');
const UserSchema = require('../models/User.js');
var { verifyToken } = require('../utils/jsonwebtoken');



module.exports = {
  async release(ctx, next) {
    var token = ctx.request.headers.token
    let token_data = verifyToken(token);
    let noteData = ctx.request.body
    const send_data = {
      author: token_data._id,
      content: noteData.content,
      imgList: noteData.imgList,
      createTime: noteData.createTime,
      place: noteData.place,
      tag: noteData.tag,
      title: noteData.title,
    }
    const result = await NoteSchema.create(send_data)  //将数据填充到 model/user.js里面
    ctx.body = {
      success: true,
      
      msg: '发布成功',
      data: result
    }
  },
  async getAllNoteByUserId(ctx, next) {
    var data = ctx.request.body
    const res = await NoteSchema.find({
      'author': data.id
    })
    ctx.body = {
      success: true,
      
      data: res
    }
  },
  async getNoteById(ctx, next) {
    let { id } = ctx.request.body;
    const res = await NoteSchema.findById(id).populate('comments.leaver').populate('author');
    ctx.body = {
      success: true,
      
      data: res
    }
  },
  async deleteNoteById(ctx, next) {
    let { id } = ctx.request.body;
    const res = await NoteSchema.deleteOne({ _id: id });
    ctx.body = {
      success: true,
      
      msg: '删除成功!'
    }
  },
  async getAllNote(ctx, next) {
    var token = ctx.request.headers.token;
    let token_data = verifyToken(token);
    // let data = await NoteSchema.find({ author: { '$ne': token_data._id } }).populate('author');
    let data = await NoteSchema.find({}).populate('author');
    ctx.body = {
      success: true,
      data
    }
  },
  async leaveWords(ctx, next) {
    let { id, comments } = ctx.request.body;
    let data = await NoteSchema.findByIdAndUpdate(id, {
      $push:
      {
        "comments": comments
      }
    })
    ctx.body = {
      success: true,
      
      msg: "留言成功!",
      data
    }
  },
  async doLike(ctx, next) {
    let { id, liker } = ctx.request.body;
    await NoteSchema.findByIdAndUpdate(id, {
      $push:
      {
        "like": liker
      }
    })
    let res = await UserSchema.findByIdAndUpdate(liker, {
      $push:
      {
        "likeNotes": id
      }
    })
    ctx.body = {
      success: true,
      
      msg: "喜欢成功!",
    }
  },
  async doUnLike(ctx, next) {
    let { id, liker } = ctx.request.body;
    await NoteSchema.findByIdAndUpdate(id, {
      $pull:
      {
        "like": liker
      }
    })
    await UserSchema.findByIdAndUpdate(liker, {
      $pull:
      {
        "likeNotes": id
      }
    })
    ctx.body = {
      success: true,
      
      msg: "取消喜欢成功!",
    }
  },
  async doCollect(ctx, next) {
    let { id, collecter } = ctx.request.body;

    let res = await UserSchema.findByIdAndUpdate(collecter, {
      $push:
      {
        "likeNotes": id
      }
    })
    ctx.body = {
      success: true,
      
      msg: "喜欢成功!",
    }
  },
}

